//
//  YLMonthlyBillController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/25.
//  Copyright © 2017年 she1323. All rights reserved.
//  月账单

#import "YLMonthlyBillController.h"
#import "YLMonthlyBillHeader.h"
#import "YLGlobeHeader.h"
#import "YLMonthlyBillCell.h"
#import "YLMonthlyBillCellHeader.h"
#import "YLMonthlyBillCalendarController.h"
#import "WHDefaultNotificationCenter.h"
#import "YLMonthlyBillViewModel.h"
#import "YLHoldingAssetsModel.h"
#import "WHNetworkTool.h"
@interface YLMonthlyBillController ()<WHDefaultNotificationCenterDelegate>
@property (nonatomic, strong) WHDefaultNotificationCenter *notiCenter;
@property (nonatomic, assign) YLMonthlyBillHeaderCenterViewType selectedType;
// 选中的时间
@property (nonatomic, strong) NSDate *selectedDate;
@property (nonatomic, weak) YLMonthlyBillHeader *header;

@property (nonatomic, strong) YLMonthlyBillViewModel *viewModel;
@end

@implementation YLMonthlyBillController

- (YLMonthlyBillViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLMonthlyBillViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    // 设置默认类型是持有资产
    _selectedType = YLMonthlyBillHeaderCenterViewTypeHoldingAssets;
    
    self.title = @"月账单";
    _notiCenter = [[WHDefaultNotificationCenter alloc] init];
    _notiCenter.delegate = self;
    
    [_notiCenter wh_addNotificationName:@"YLChangeTimeNoti"];
    [_notiCenter wh_addNotificationName:@"YLMonthlyBillCalendar"];
    [_notiCenter wh_addNotificationName:@"YLMonthlyChangeType"];
    
    YLMonthlyBillHeader *header = [[YLMonthlyBillHeader alloc] init];
    
    _header = header;
    // 默认是持有资产
    header.type = _selectedType;
    header.frame = CGRectMake(0, 0, YLScreenW, 234);
    self.tableView.tableHeaderView = header;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLMonthlyBillCell class]) bundle:nil] forCellReuseIdentifier:[YLMonthlyBillCell cellID]];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // 选中日期默认为今天
    //_selectedDate = [NSDate date];
    
   // [self loadData];
    
}

- (void)defaultNotificationCenter:(WHDefaultNotificationCenter *)notification name:(NSString *)name object:(id)object
{
    if ([name isEqualToString:@"YLChangeTimeNoti"]) {
        // 修改了时间
        NSDate *date = object;
        _selectedDate = date;
        [self loadData];
    } else if ([name isEqualToString:@"YLMonthlyBillCalendar"]) {
        // 弹出日历
        [self monthlyBillCalendar];
        
    } else if ([name isEqualToString:@"YLMonthlyChangeType"]) {
        // 切换类型
        _selectedType = (YLMonthlyBillHeaderCenterViewType)[object integerValue];
        _header.type = _selectedType;
        [self loadData];
    }
}


- (void)loadData
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString *date = [dateFormatter stringFromDate:_selectedDate];
    
    // 持有资产
    if (_selectedType == YLMonthlyBillHeaderCenterViewTypeHoldingAssets) {
        [self.viewModel.holdingAssetsCommand execute:date];
        [self.viewModel.returnOnAccountCommand execute:date];
    } else {
        // 预期收益
        [self.viewModel.expectedReturnCommand execute:date];
        [self.viewModel.returnOnAccountCommand execute:date];
    }
    
    __weak typeof(self) weakSelf = self;
    [self.viewModel.holdingAssetsSignal subscribeNext:^(id  _Nullable x) {
        if ([x boolValue]) {
            [weakSelf.tableView reloadData];
        }
    }];
    
    [self.viewModel.expectedReturnSignal subscribeNext:^(id  _Nullable x) {
        if ([x boolValue]) {
            [weakSelf.tableView reloadData];
        }
    }];
    
    
}

- (void)monthlyBillCalendar
{
    YLMonthlyBillCalendarController *calendar = [[YLMonthlyBillCalendarController alloc] init];
    calendar.view.frame = [UIScreen mainScreen].bounds;
    
    [[UIApplication sharedApplication].keyWindow addSubview:calendar.view];
    
    [self addChildViewController:calendar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.viewModel.group.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    YLHoldingAssetsItem *item = self.viewModel.group[section];
    
    return item.item.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YLMonthlyBillCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLMonthlyBillCell cellID] forIndexPath:indexPath];
    YLHoldingAssetsItem *item = self.viewModel.group[indexPath.section];
    cell.model = item.item[indexPath.row];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    YLMonthlyBillCellHeader *header = [[YLMonthlyBillCellHeader alloc] init];
    YLHoldingAssetsItem *item = self.viewModel.group[section];
    header.title = item.title;
    header.subTitle = item.totalTitle;
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (void)dealloc
{
    
    [[WHNetworkTool shareInstance] cancel];
    [_notiCenter wh_removeAllNotifications];
}



@end
